package com.wwwarehouse.usercenter.fragment.invite;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.eventbus_event.F2FInviteEvent;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.common.tools.QRCodeUtils;
import com.wwwarehouse.usercenter.R;
import com.wwwarehouse.usercenter.bean.PushReceiverF2FBean;
import com.wwwarehouse.usercenter.bean.response.SendInviteResponseBean;
import com.wwwarehouse.usercenter.constant.UserCenterConstant;

import de.greenrobot.event.EventBus;

/**
 * 面对面邀请
 * Created by Jackie on 2017/8/9.
 */

public class InviteRegisterF2FFragment extends BaseFragment implements View.OnClickListener {
    private View mRootView;
    private ImageView mQrCodeImg;
    private TextView mInviteCodeTxt;
    private LinearLayout mInviteLayout;
    private TextView mInviteCountTxt;
    private LinearLayout mHeadLayout;
    private TextView mShowInvitedTxt;

    private int mInviteCount = 0;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mRootView = inflater.inflate(R.layout.fragment_invit_register_f2f, container, false);
        return mRootView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initEvent();
    }

    private void initView() {
        EventBus.getDefault().register(this);
        mQrCodeImg = findView(mRootView, R.id.iv_qr_code);
        mInviteCodeTxt = findView(mRootView, R.id.tv_invite_code);
        mInviteLayout = findView(mRootView, R.id.ll_invite);
        mInviteCountTxt = findView(mRootView, R.id.tv_invite_count);
        mHeadLayout = findView(mRootView, R.id.ll_head);
        mShowInvitedTxt = findView(mRootView, R.id.tv_show_invited);

        if (getArguments() != null) {
            SendInviteResponseBean sendInviteResponseBean = (SendInviteResponseBean) getArguments().getSerializable(UserCenterConstant.KEY_REGISTER_SEND_INVITE);
            refreshQRCode(sendInviteResponseBean.getInviteUrl(),sendInviteResponseBean.getInviteCode());
        }
    }

    private void initEvent() {
        mShowInvitedTxt.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.tv_show_invited) {
            pushFragment(new ShowInvitedFragment());
        }
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof InviteRegisterF2FFragment) {
            mActivity.setTitle(R.string.invite_register_f2f);
        }
    }

    @Override
    public void requestDatas() {

    }

    //面对面邀请
    public void onEventMainThread(F2FInviteEvent event) {
        String message = event.getMsg();
        PushReceiverF2FBean pushReceiverF2FBean = JSON.parseObject(message, PushReceiverF2FBean.class);

        /**
         * 扫码成功后，加载h5页面，H5给后台发请求，然后后台以通知的形式发送新的邀请码给客户端，客户端刷新二维码
         * 然后执行下面的代码，被邀请的人数小于等于6，显示头像，大于6，显示文本
         */
        refreshQRCode(pushReceiverF2FBean.getMsg().getInviteUrl(), pushReceiverF2FBean.getMsg().getInviteCode());
        mInviteCount++;

        if (mInviteCount <= 6) {
            mInviteLayout.setVisibility(View.GONE);
            mHeadLayout.setVisibility(View.VISIBLE);

            ImageView imageView = new ImageView(mActivity);
            imageView.setImageResource(R.drawable.f2f_head);
            mHeadLayout.addView(imageView);

            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageView.getLayoutParams();

            params.width = ConvertUtils.dip2px(mActivity, 34);
            params.height = ConvertUtils.dip2px(mActivity, 34);

            if (mInviteCount > 1) {
                params.leftMargin = ConvertUtils.dip2px(mActivity, 15);
            }

            imageView.setLayoutParams(params);
        } else {
            mInviteCountTxt.setText(mInviteCount + "");
            mInviteLayout.setVisibility(View.VISIBLE);
            mHeadLayout.setVisibility(View.GONE);
        }
    }

    private void refreshQRCode(String inviteUrl, String inviteCode){
        Bitmap bitmap = QRCodeUtils.createQRCode(inviteUrl);
        mQrCodeImg.setImageBitmap(bitmap);

        mInviteCodeTxt.setText(inviteCode);
    }
}
